package com.lysj.bhp.approve.controller;

import com.lysj.bhp.approve.base.ApproveContent;
import com.lysj.bhp.approve.domain.ApproveRecord;
import com.lysj.bhp.approve.vo.ApproveRecordVo;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/8/21--17:37
 * @Description:认证档案控制类
 */
@RestController
@RequestMapping("/admin/approve_record")
public class ApproveRecordController extends ApproveContent {

    /**
     * @author drj
     * @date 2018/9/6 10:16
     * @Description:身份证申请界面
     */
    @GetMapping("/id_card_apply_list")
    public Resp idCardApplyList() {
        List<ApproveRecord> approveRecordList = approveRecordRepository.findByIdCardStatusOrderByIdCardRenewalDateDesc(2);
        return new Resp(approveRecordList);
    }

    /**
     * @author drj
     * @date 2018/9/6 10:17
     * @Description：身份证审核操作
     */
    @PostMapping("/id_card_approval")
    public Resp idCardApproval(ApproveRecord model) {
        ApproveRecord approveRecord = approveRecordRepository.findOne(model.getApproveRecordId());
        if (approveRecord == null) {
            return new Resp(Resp.PARAM_ERROR, "修改失败,该申请信息不存在!");
        }
        approveRecord.setIdCardStatus(model.getIdCardStatus());
        //若选择的是审核通过,认证等级更新为2级
        if (1 == model.getIdCardStatus()) {
            approveRecord.setInfoStatus(2);
        }
        approveRecord.setIdCardRenewalDate(new Date());
        approveRecordRepository.save(approveRecord);
        return new Resp("操作成功!");
    }

    /**
     * @author drj
     * @date 2018/9/6 10:17
     * @Description:身份证审核记录
     */
    @GetMapping("/id_card_approve_details")
    public Resp idCardApproveDetails() {
        Integer[] typeIds = new Integer[2];
        typeIds[0] = -1;
        typeIds[1] = 1;
        List<ApproveRecord> approveRecordList = approveRecordRepository.findByIdCardStatusInOrderByIdCardRenewalDateDesc(typeIds);
        return new Resp(approveRecordList);
    }


    /**
     * @author drj
     * @date 2018/9/6 10:28
     * @Description:护照申请记录
     */
    @GetMapping("/ppt_apply_list")
    public Resp pptApplyList() {
        List<ApproveRecord> approveRecordList = approveRecordRepository.findByPptStatusOrderByPptRenewalDateDesc(2);
        return new Resp(approveRecordList);
    }

    /**
     * @author drj
     * @date 2018/9/6 10:17
     * @Description：护照审核操作
     */
    @PostMapping("/ppt_approval")
    public Resp pptApproval(ApproveRecord model) {
        ApproveRecord approveRecord = approveRecordRepository.findOne(model.getApproveRecordId());
        if (approveRecord == null) {
            return new Resp(Resp.PARAM_ERROR, "修改失败,该申请信息不存在!");
        }
        approveRecord.setPptStatus(model.getPptStatus());
        //若选择的是审核通过,认证等级更新为2级
        if (1 == model.getPptStatus()) {
            approveRecord.setInfoStatus(2);
        }
        approveRecord.setPptRenewalDate(new Date());
        approveRecordRepository.save(approveRecord);
        return new Resp("操作成功!");
    }

    /**
     * @author drj
     * @date 2018/9/6 10:17
     * @Description:护照审核记录
     */
    @GetMapping("/ppt_approve_details")
    public Resp pptApproveDetails() {
        Integer[] typeIds = new Integer[2];
        typeIds[0] = -1;
        typeIds[1] = 1;
        List<ApproveRecord> approveRecordList = approveRecordRepository.findByPptStatusInOrderByPptRenewalDateDesc(typeIds);
        return new Resp(approveRecordList);
    }


    /**
     * @author drj
     * @date 2018/9/6 10:28
     * @Description: 手持照申请记录
     */
    @GetMapping("/mlp_apply_list")
    public Resp mlpApplyList() {
        List<ApproveRecord> approveRecordList = approveRecordRepository.findByMultipleStatusOrderByMultipleRenewalDateDesc(2);
        return new Resp(approveRecordList);
    }

    /**
     * @author drj
     * @date 2018/9/6 10:17
     * @Description： 手持照审核操作
     */
    @PostMapping("/mlp_approval")
    public Resp mlpApproval(ApproveRecord model) {
        ApproveRecord approveRecord = approveRecordRepository.findOne(model.getApproveRecordId());
        if (approveRecord == null) {
            return new Resp(Resp.PARAM_ERROR, "修改失败,该申请信息不存在!");
        }
        approveRecord.setMultipleStatus(model.getMultipleStatus());
        //若选择的是审核通过,认证等级更新为3级
        if (1 == model.getMultipleStatus()) {
            approveRecord.setInfoStatus(3);
        }
        approveRecord.setMultipleRenewalDate(new Date());
        approveRecordRepository.save(approveRecord);
        return new Resp("操作成功!");
    }

    /**
     * @author drj
     * @date 2018/9/6 10:17
     * @Description: 手持照审核记录
     */
    @GetMapping("/mlp_approve_details")
    public Resp mlpApproveDetails() {
        Integer[] typeIds = new Integer[2];
        typeIds[0] = -1;
        typeIds[1] = 1;
        List<ApproveRecord> approveRecordList = approveRecordRepository.findByMultipleStatusInOrderByMultipleRenewalDateDesc(typeIds);
        return new Resp(approveRecordList);
    }

    @GetMapping("/show_user_info")
    public Resp showUserInfo(Long userId) {
        if (ParamUtil.isBlack(userId)) {
            return new Resp(Resp.PARAM_ERROR, "系统内部错误");
        }
        return new Resp(userContent.userRepository.findByUserId(userId));
    }

}
